'use strict';

const Controller = require('egg').Controller;

const path=require("path")
const fs=require("fs")

const {sendMail}=require("../utils/sendMail")

class HomeController extends Controller {

  // 上传邮箱
  async getPwd(){
    const {ctx}=this
    const {id,email}=ctx.request.body
    // 发送邮件
    const result=await sendMail(email,"王泉清你是不是傻","王泉清:是")
    // console.log(result);
    if(result.code===200){
      ctx.body={
        code:"1001",
        msg:"发送邮箱成功"
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"发送邮箱失败"
      }
    }
  }

  // oss 切片上传
  async ossupload(){
    const {ctx}=this
    const file=ctx.request.files[0]
    // console.log(file);
    // name 为Bucket列表目录名   创建名字是加上时间戳
    const name = '1907C/'+new Date()*1 + path.basename(file.filename);
    let result;
    try {
      result = await ctx.oss.put(name, file.filepath);
      // console.log(result);
    } finally {
      // 更改为同步
      await fs.unlinkSync(file.filepath);
    }
 
    if (result) {
      if(result.res.status===200){
        const res=await ctx.service.user.upload({
          id:ctx.request.query.id,
          imgUrl:result.url
        })
        // console.log(res);
        if(res.affectedRows){
          ctx.body={
            code:"1001",
            msg:"oss上传成功",
            imgUrl:result.url
          }
        }else{
          ctx.body={
            code:"1002",
            msg:"oss上传失败"
          }
        }
        
      }else{
        ctx.body={
          code:"1002",
          msg:"oss上传失败"
        }
      }
    } else {
      ctx.body = 'please select a file to upload';
    }
  }


  // 上传文件  本地
  async upload(){
    const {ctx}=this
    // 获取到文件的信息
    const file=ctx.request.files[0]
    const {filename,filepath}=file
    const imgUrl=await ctx.service.user.saveAvatar(filename,filepath)
    // console.log(imgUrl)  //http://localhost:7001/public/avatar/t24.jpg
    if(imgUrl){
      // 保存到mysql 里面
      const result=await ctx.service.user.upload({
        id:ctx.request.query.id,
        imgUrl
      })
      console.log(result);
      if(result.affectedRows){
        ctx.body={
          code:"1001",
          msg:"上传成功",
          imgUrl
        }
      }else{
        ctx.body={
          code:"1002",
          msg:"上传失败"
        }
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"获取不到图片信息"
      }
    }
  }

  // 第三方登录
  async getUserInfo(){
    const {ctx}=this
    const {access_token}=ctx.request.query
    const result=await this.service.user.get_user_info(access_token)
    if(result.status==200){
      ctx.body={
        code:"1001",
        msg:"获取用户信息成功",
        data:result.data
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"获取用户信息失败"
      }
    }
  }
  
  async getCode() {
    const { ctx } = this;
    // 接受前端发送过来的code码
    const {code}=ctx.request.query
    const result=await ctx.service.user.get_access_token(code)
    if(result.status==200){
      ctx.body={
        code:"1001",
        msg:"获取access_token 成功",
        access_token:result.data.access_token,
        isAbandon:true
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"获取access_token 失败",
        isAbandon:true
      }
    }
  }
}

module.exports = HomeController;
